rm(list = setdiff(ls(), lsf.str()))
#load data
load("Study 1/Altered Data/Study1_NL_17.RData")

#SI A.7: models belonging to Figure 1-------------------
vote.17<-list()
vote.17[[1]]<-glm(populist ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age, data=data, family="binomial")
vote.17[[2]]<-glm(populist ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age+ edu2 + edu3 + edu4 + edu5 + edu6 + income + income_missing + zero1(econ_cons) + zero1(anti_immi) + zero1(cynicism) , data=data, family="binomial")
vote.17[[3]]<-glm(populist_pvv ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age, data=data, family="binomial")
vote.17[[4]]<-glm(populist_pvv ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age+ edu2 + edu3 + edu4 + edu5 + edu6 + income + income_missing + zero1(econ_cons) + zero1(anti_immi) + zero1(cynicism) , data=data, family="binomial")
labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age",  "Education (Ref: primary): Intermediate secondary education", "Higher secondary education", "Intermediate vocational education", "Higher vocational education", "University", "Income", "Income missing", "Economic conservatism","Social conservatism",  "Political cynicism")
stargazer2(vote.17, odd.ratio=TRUE, title="Dutch Election Study 2017: Support for Populist Parties", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"),  star.cutoffs=c(0.05), covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1", "PVV only base", "PVV only full"), notes = "Odds ratios with standard errors from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/NL17_results.tex", no.space=TRUE, label="tab:NL17_results" )

#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

# Odds difference in full model belonging to FIgure 1
odds.difference <- (exp(vote.17[[2]]$coefficients)[2]*low) / (exp(vote.17[[2]]$coefficients)[2]*high)
1/odds.difference

#SI A.7: Descriptive statistics----------------------
myvars <- c("populist", "agre", "open", "con", "ext", "neu", "female", "age", "income", "income_missing", "edu1", "edu2", "edu3", "edu4", "edu5", "edu6", "cynicism" , "anti_immi" , "econ_cons")
newdata <- data[myvars]
desc.labels <- c("Populist Vote",labels)
stargazer(vote.17[[2]]$model, type = "latex", covariate.labels = desc.labels, summary.stat = c("mean", "sd", "N", "median","min",  "max"), title="Descriptive statistics Dutch Election Study 2017", out="Tables/NL_descrip17.tex", no.space=TRUE, label="tab:NL_descriptives17", digits=2)

#Alpha Agreeableness
data_a<-data.frame(data$y17_agre1,data$y17_agre2,data$y17_agre3,data$y17_agre4,data$y17_agre5,data$y17_agre6, data$y17_rec_agre7, data$y17_rec_agre8, data$y17_rec_agre9, data$y17_rec_agre10)
psych::alpha(data_a)
#Alpha Openness
data_o<-data.frame(data$y17_open1,data$y17_open2,data$y17_open3,data$y17_open4,data$y17_open5,data$y17_open6, data$y17_open7, data$y17_rec_open8, data$y17_rec_open9, data$y17_rec_open10)
psych::alpha(data_o)
#Alpha Conscientiousness
data_c<-data.frame(data$y17_con1,data$y17_con2,data$y17_con3,data$y17_con4,data$y17_con5,data$y17_con6, data$y17_rec_con7, data$y17_rec_con8, data$y17_rec_con9, data$y17_rec_con10)
psych::alpha(data_c)
#Alpha Extraversion
data_e<-data.frame(data$y17_ext1,data$y17_ext2,data$y17_ext3,data$y17_ext4,data$y17_ext5,data$y17_rec_ext6, data$y17_rec_ext7, data$y17_rec_ext8, data$y17_rec_ext9, data$y17_rec_ext10)
psych::alpha(data_e)
#Alpha Neuroticism
data_n<-data.frame(data$y17_neu1,data$y17_neu2,data$y17_neu3,data$y17_neu4,data$y17_neu5,data$y17_neu6, data$y17_neu7, data$y17_neu8, data$y17_rec_neu9, data$y17_rec_neu10)
psych::alpha(data_n)
#Alpha cynicism
cyn_a<-data.frame(data$cyn1,data$cyn2,data$cyn3)
psych::alpha(cyn_a)

median(as.numeric(data$nettocat)[as.numeric(data$nettocat)<13], na.rm=T)

#SI A.7: Correlation matrix-------------------
myvars <- c("agre", "open", "con", "ext", "neu", "cynicism" , "anti_immi" , "econ_cons")
cor_ivs <- data[myvars]
names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", "6. Cynicism",
                    "7. Social conservatism", "8. Economic conservatism")
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Dutch Election Study 2017: Correlation Matrix of Independent Variables", out="Tables/NL_cor17.tex", no.space=TRUE, label="tab:NL_cor17", digits=2)

#SI A.7: Factor loadings ----------------
latent<-(with(data, data.frame(y17_agre1, y17_agre2, y17_agre3, y17_agre4, y17_agre5, y17_agre6, y17_rec_agre7, y17_rec_agre8, y17_rec_agre9, y17_rec_agre10, y17_open1 , y17_open2, y17_open3, y17_open4, y17_open5, y17_open6, y17_open7, y17_rec_open8, y17_rec_open9, y17_rec_open10, y17_con1, y17_con2, y17_con3, y17_con4, y17_con5, y17_con6, y17_rec_con7, y17_rec_con8, y17_rec_con9, y17_rec_con10, y17_ext1, y17_ext2, y17_ext3, y17_ext4, y17_ext5, y17_rec_ext6, y17_rec_ext7, y17_rec_ext8, y17_rec_ext9, y17_rec_ext10, y17_neu1, y17_neu2, y17_neu3, y17_neu4, y17_neu5, y17_neu6, y17_neu7, y17_neu8, y17_rec_neu9, y17_rec_neu10)))
latent <- na.omit(latent)
CFA_agree <-'Agree = ~ NA*y17_agre1+ y17_agre2+ y17_agre3+ y17_agre4+ y17_agre5+ y17_agre6+ y17_rec_agre7+ y17_rec_agre8+ y17_rec_agre9+ y17_rec_agre10 
Open = ~ NA*y17_open1+y17_open2+ y17_open3+ y17_open4+ y17_open5+ y17_open6+ y17_open7+ y17_rec_open8+ y17_rec_open9+ y17_rec_open10
Con = ~ NA*y17_con1+ y17_con2+ y17_con3+ y17_con4+ y17_con5+ y17_con6+ y17_rec_con7+ y17_rec_con8+ y17_rec_con9+ y17_rec_con10
Ext = ~ NA* y17_ext1+ y17_ext2+ y17_ext3+ y17_ext4+ y17_ext5+ y17_rec_ext6+ y17_rec_ext7+ y17_rec_ext8+ y17_rec_ext9+ y17_rec_ext10
Neu = ~ NA*y17_neu1+ y17_neu2+ y17_neu3+ y17_neu4+ y17_neu5+ y17_neu6+ y17_neu7+ y17_neu8+ y17_rec_neu9+ y17_rec_neu10
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 
fit<-cfa(CFA_agree, data=latent)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:50, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
(setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Agreeableness item 4", "Agreeableness item 5", "Agreeableness item 6", "Agreeableness item 7", "Agreeableness item 8", "Agreeableness item 9", "Agreeableness item 10", "Openness item 1", "Openness item 2", "Openness item 3", "Openness item 4", "Openness item 5", "Openness item 6", "Openness item 7", "Openness item 8", "Openness item 9", "Openness item 10", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3", "Conscientiousness item 4", "Conscientiousness item 5", "Conscientiousness item 6", "Conscientiousness item 7", "Conscientiousness item 8", "Conscientiousness item 9", "Conscientiousness item 10",  "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Extraversion item 4", "Extraversion item 5", "Extraversion item 6", "Extraversion item 7", "Extraversion item 8", "Extraversion item 9", "Extraversion item 10", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item 3", "Neuroticism item 4", "Neuroticism item 5", "Neuroticism item 6", "Neuroticism item 7", "Neuroticism item 8", "Neuroticism item 9", "Neuroticism item 10")))
p<-xtable(caption = "Dutch Election Study 2017: Big Five Standardized Factor Loadings", label = "tab:NL17_cfa", p)
print(p, type="latex", file="Tables/NL_factor17.tex", size="tiny", caption.placement="top")


